<template lang="html">
  <div v-loading="loading" class="main-panel">
    <el-button type="primary" @click="groupDialog('post')">添加</el-button>
    <el-dialog v-loading="loading" title="编辑" :visible.sync="groupDiaIsShow" class="diaForm" :append-to-body="false">
      <el-form
        ref="groupRef"
        :model="groupForm"
        :rules="groupRules"
        label-width="140px"
      >
        <el-form-item label="媒体分组名称" prop="media_group_name">
          <el-col :span="15">
            <el-input
              v-model="groupForm.media_group_name"
              type="text"
              placeholder="请输入媒体分组名称"
            />
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="editGroup()"
          >确认
          </el-button>
          <el-button @click="groupDiaIsShow = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-table
      :data="mediaGroupTableData"
      border
      style="width: 100%"
    >
      <el-table-column
        prop="name"
        label="媒体分组名称"
      />
      <el-table-column
        prop="create_time"
        label="创建时间"
      />
      <el-table-column
        prop="update_time"
        label="更新时间"
      />
      <el-table-column
        fixed="right"
        label="操作"
        width="100"
        align="center"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="groupDialog('put',scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteGroup(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align:center;margin-top:20px">
      <el-pagination
        :current-page="mediaGroupTableDataPaginateCurrentPage"
        :page-sizes="mediaGroupTableDataPaginateTotalPageSizes"
        :page-size="mediaGroupTableDataPaginateTotalPageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="mediaGroupTableDataPaginateTotal"
        @size-change="mediaGroupTableDataHandleSizeChange"
        @current-change="mediaGroupTableDataHandleCurrentChange"
      >
        <!-- <span>当前{{ mediaGroupTableDataPaginateCurrentPage }}页，</span>
                        <span>共{{ mediaGroupTableDataPaginateLastPage }}页</span> -->
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      mediaGroupSearchForm: {},
      mediaGroupTableData: [],
      mediaGroupTableDataPaginateCurrentPage: 1,
      mediaGroupTableDataPaginateTotal: 1,
      mediaGroupTableDataPaginateLastPage: 1,
      mediaGroupTableDataPaginateTotalPageSize: 10,
      mediaGroupTableDataPaginateTotalPageSizes: [10, 20, 50, 100],
      groupTab: [],
      groupEditType: 'post',
      groupDiaIsShow: false,
      groupForm: {},
      groupRules: {
        mobile_number: [{
          required: true,
          trigger: 'blur'
        }],
        password: [{
          required: true,
          trigger: 'blur'
        }]
      }
    }
  },
  created() {
    this.getGroupList()
  },
  methods: {
    mediaGroupTableDataHandleSizeChange(val) {
      // 改变每页显示的条数
      this.mediaGroupTableDataPaginateTotalPageSize = val
      this.getGroupList(1, this.mediaGroupTableDataPaginateTotalPageSize)
    },
    mediaGroupTableDataHandleCurrentChange(val) {
      this.mediaGroupTableDataPaginateCurrentPage = val
      this.getGroupList(val, this.mediaGroupTableDataPaginateTotalPageSize)
    },
    getGroupList(page = 1, limit = 10) {
      this.loading = true
      this.mediaGroupSearchForm.page = page
      this.mediaGroupSearchForm.limit = limit
      this.$store.dispatch('media/getMediaGroupList', this.mediaGroupSearchForm).then((res) => {
        this.mediaGroupTableData = res.data.data
        this.mediaGroupTableDataPaginateTotal = res.data.total
        this.mediaGroupTableDataPaginateCurrentPage = res.data.current_page
        this.mediaGroupTableDataPaginateLastPage = res.data.last_page
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    deleteGroup(row) {
      // console.log(row)
      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true
          this.$store.dispatch('media/deleteMediaGroup', { media_group_id: row.id }).then((res) => {
            this.getGroupList()
            this.loading = false
            this.groupDiaIsShow = false
            this.$message({
              type: 'success',
              message: res.msg
            })
          }).catch(() => {
            this.loading = false
            return false
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    editGroup() {
      switch (this.groupEditType) {
        case 'post':
          this.$refs['groupRef'].validate((valid) => {
            if (valid) {
              this.loading = true
              this.$store.dispatch('media/postMediaGroup', this.groupForm).then((res) => {
                this.getGroupList()
                this.groupForm = {}
                this.loading = false
                this.groupDiaIsShow = false
                this.$message({
                  type: 'success',
                  message: res.msg
                })
              }).catch(() => {
                this.loading = false
              })
            } else {
              return false
            }
          })
          break
        case 'put':
          this.$refs['groupRef'].validate((valid) => {
            if (valid) {
              this.loading = true
              this.$store.dispatch('media/putMediaGroup', this.groupForm).then((res) => {
                this.getGroupList()
                this.groupForm = {}
                this.loading = false
                this.groupDiaIsShow = false
                this.$message({
                  type: 'success',
                  message: res.msg
                })
              }).catch(() => {
                this.loading = false
              })
            } else {
              return false
            }
          })
          break
      }
    },
    groupDialog(type, row) {
      this.groupDiaIsShow = true
      switch (type) {
        case 'post':
          this.groupEditType = 'post'
          break
        case 'put':
          this.groupEditType = 'put'
          // 这里一定要使用nextTick的方式赋值，不然会卡住input
          this.$nextTick(() => {
            this.$set(this.groupForm, 'media_group_id', row.id)
            this.$set(this.groupForm, 'media_group_name', row.name)
          })
          break
        default:
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.main-panel {
  padding: 18px;
  font-size: 10px;
}
</style>
